﻿using System;
using System.Collections.Generic;
using System.Text;
using DALFactory.DataBase;
using AppToolKit.Core.Context.Spring;
using System.Data;
using Utility;

namespace BLL.Business
{
    [AutoWiring]
    public class AllotMentBuesinessDao
    {
        [AutoWiring]
        public AccessHelper AccessHelperHolder { get; set; }

        public void AddTaskList(List<string> taskLists,string hotelid)
        {
            
        }



        internal System.Data.DataSet GetTaskAllot(string taskListID)
        {
            string strSql = string.Format("select t.code,t.descript from task  t,allot_task a where a.task_code=t.code and a.tasklist_code='{0}'", taskListID);
            return AccessHelperHolder.GetDataSet(System.Data.CommandType.Text, strSql);
        }
        internal DataSet GetTask(string taskListID)
        {
            string strSql = string.Format("select * from task t where t.code not in (select task_code from allot_task where tasklist_code='{0}')",taskListID);
            return AccessHelperHolder.GetDataSet(System.Data.CommandType.Text, strSql);

        }

        internal void AddTaskAllot(string listID, string taskID)
        {
            string strSql = string.Format("insert into allot_task (id,tasklist_code,task_code) values('{0}','{1}','{2}')", CreateTaskAllotID("TA", "allot_task"), listID, taskID);
            AccessHelperHolder.GetDataSet(System.Data.CommandType.Text, strSql);
        }
        internal void DelTaskAllot(string listID, string taskID)
        {
            string strSql=string.Empty ;
            if (taskID.Trim().Length >0)
                strSql = string.Format("delete allot_task where tasklist_code='{0}' and task_code='{1}'",listID ,taskID );
            else
                strSql = string.Format("delete allot_task where tasklist_code='{0}'", listID);

            AccessHelperHolder.ExecuteNonQuery(System.Data.CommandType.Text, strSql);
        }
        private string CreateTaskAllotID(string p,string table)
        {
            string retID = string.Format("{0}00000001",p.ToUpper());
            string strSql = string.Format("select max(id) from {0}",table);
            object id=AccessHelperHolder.ExecuteScalar(strSql);
            if (id != null && !StringHelper.IsEmty(id))
            {
               int length=Convert.ToInt32(id.ToString().Substring(2));
               length += 1;
                retID=string.Format("{1}{0}",length.ToString().PadLeft(8,'0'),p.ToUpper());
            }
            return retID;
        }

        internal DataSet GetAllTaskListAlloment(string hotelid)
        {
           string strSql= string.Format("select tasklist_code code from allot_tasklist where hotelid='{0}'",hotelid);
          return  AccessHelperHolder.GetDataSet(System.Data.CommandType.Text, strSql);
        }

        internal void DelTaskListAllot(string hotelid, string p)
        {
            string strSql = string.Format("delete from allot_tasklist where hotelid='{0}' and tasklist_code='{1}'", hotelid,p);
             AccessHelperHolder.ExecuteNonQuery(System.Data.CommandType.Text, strSql);
        }

        internal void Insert(string hotelid, string key)
        {
            string strSql = string.Format("INSERT INTO  allot_tasklist VALUES ('{0}','{1}','{2}','{3}')",CreateTaskAllotID("HA","allot_tasklist"),hotelid,key,"true");
            AccessHelperHolder.ExecuteNonQuery(System.Data.CommandType.Text, strSql);
        }

        internal bool Exist(string hotelid, string key)
        {
            string strSql = string.Format("select count(*) from allot_tasklist where hotelid='{0}' and tasklist_code='{1}'",  hotelid, key);
           object o= AccessHelperHolder.ExecuteScalar(strSql);
           if (Convert.ToInt32(o) == 0)
               return false;
           return true;
        }
    }
}
